// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use crypto::ec;
use bytes;
use memio;

@test fn p256_newkey() void = {
	let priv: [_]u8 = [
		0xde, 0x5c, 0x88, 0x05, 0x42, 0xa0, 0x71, 0xe2, 0xf6, 0xfe,
		0xd0, 0xdc, 0x80, 0x07, 0x37, 0xc4, 0x35, 0xa6, 0x29, 0x48,
		0x85, 0x70, 0x4f, 0x54, 0x1c, 0x41, 0x89, 0xaf, 0xf6, 0xbc,
		0xb5, 0x19,
	];

	let key = p256();
	newkey(&key, &memio::fixed(priv))!;
};

@test fn p256_invalidpoint() void = {
	let priv = p256();
	priv.x[..] = [
		0x7d, 0x7d, 0xc5, 0xf7, 0x1e, 0xb2, 0x9d, 0xda, 0xf8, 0x0d,
		0x62, 0x14, 0x63, 0x2e, 0xea, 0xe0, 0x3d, 0x90, 0x58, 0xaf,
		0x1f, 0xb6, 0xd2, 0x2e, 0xd8, 0x0b, 0xad, 0xb6, 0x2b, 0xc1,
		0xa5, 0x34,
	];

	const pub: [_]u8 = [
		0x04, 0xea, 0xd2, 0x18, 0x59, 0x01, 0x19, 0xe8, 0x87, 0x6b,
		0x29, 0x14, 0x6f, 0xf8, 0x9c, 0xa6, 0x17, 0x70, 0xc4, 0xed,
		0xbb, 0xf9, 0x7d, 0x38, 0xce, 0x38, 0x5e, 0xd2, 0x81, 0xd8,
		0xa6, 0xb2, 0x30, 0x28, 0xaf, 0x61, 0x28, 0x1f, 0xd3, 0x5e,
		0x2f, 0xa7, 0x00, 0x25, 0x23, 0xac, 0xc8, 0x5a, 0x42, 0x9c,
		0xb0, 0x6e, 0xe6, 0x64, 0x83, 0x25, 0x38, 0x9f, 0x59, 0xed,
		0xfc, 0xe1, 0x40, 0x51, 0x41,
	];

	const otherpub: [_]u8 = [
		0x04, 0x70, 0x0c, 0x48, 0xf7, 0x7f, 0x56, 0x58, 0x4c, 0x5c,
		0xc6, 0x32, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x1b, 0x6b, 0xac,
		0xce, 0x3a, 0x4d, 0xf6, 0xb4, 0x2c, 0xe7, 0xcc, 0x83, 0x88,
		0x33, 0xd2, 0x00, 0xdb, 0x71, 0xe5, 0x09, 0xe3, 0xfd, 0x9b,
		0x06, 0x0d, 0xdb, 0x20, 0xba, 0x5c, 0x51, 0xdc, 0xc5, 0x94,
		0x8d, 0x46, 0xfb, 0xf6, 0x40, 0xdf, 0xe0, 0x44, 0x17, 0x82,
		0xca, 0xb8, 0x5f, 0xa4, 0xac,
	];

	const shared: [_]u8 = [
		0x46, 0xfc, 0x62, 0x10, 0x64, 0x20, 0xff, 0x01, 0x2e, 0x54,
		0xa4, 0x34, 0xfb, 0xdd, 0x2d, 0x25, 0xcc, 0xc5, 0x85, 0x20,
		0x60, 0x56, 0x1e, 0x68, 0x04, 0x0d, 0xd7, 0x77, 0x89, 0x97,
		0xbd, 0x7b,
	];

	let dpub: [ec::P256_POINTSZ]u8 = [0...];
	pubkey(dpub, &priv);
	assert(bytes::equal(dpub, pub));

	let dshared: [P256_SHAREDSZ]u8 = [0...];
	let zero: [P256_SHAREDSZ]u8 = [0...];

	assert(derive(dshared, &priv, otherpub) is invalidkey);
	assert(bytes::equal(dshared, zero));
};

@test fn p384_invalidpoint() void = {
	let priv = p384();
	priv.x[..] = [
		0x3c, 0xc3, 0x12, 0x2a, 0x68, 0xf0, 0xd9, 0x50, 0x27, 0xad,
		0x38, 0xc0, 0x67, 0x91, 0x6b, 0xa0, 0xeb, 0x8c, 0x38, 0x89,
		0x4d, 0x22, 0xe1, 0xb1, 0x56, 0x18, 0xb6, 0x81, 0x8a, 0x66,
		0x17, 0x74, 0xad, 0x46, 0x3b, 0x20, 0x5d, 0xa8, 0x8c, 0xf6,
		0x99, 0xab, 0x4d, 0x43, 0xc9, 0xcf, 0x98, 0xa1,
	];
	const pub: [_]u8 = [
		0x04, 0x98, 0x03, 0x80, 0x7f, 0x2f, 0x6d, 0x2f, 0xd9, 0x66,
		0xcd, 0xd0, 0x29, 0x0b, 0xd4, 0x10, 0xc0, 0x19, 0x03, 0x52,
		0xfb, 0xec, 0x7f, 0xf6, 0x24, 0x7d, 0xe1, 0x30, 0x2d, 0xf8,
		0x6f, 0x25, 0xd3, 0x4f, 0xe4, 0xa9, 0x7b, 0xef, 0x60, 0xcf,
		0xf5, 0x48, 0x35, 0x5c, 0x01, 0x5d, 0xbb, 0x3e, 0x5f, 0xba,
		0x26, 0xca, 0x69, 0xec, 0x2f, 0x5b, 0x5d, 0x9d, 0xad, 0x20,
		0xcc, 0x9d, 0xa7, 0x11, 0x38, 0x3a, 0x9d, 0xbe, 0x34, 0xea,
		0x3f, 0xa5, 0xa2, 0xaf, 0x75, 0xb4, 0x65, 0x02, 0x62, 0x9a,
		0xd5, 0x4d, 0xd8, 0xb7, 0xd7, 0x3a, 0x8a, 0xbb, 0x06, 0xa3,
		0xa3, 0xbe, 0x47, 0xd6, 0x50, 0xcc, 0x99,
	];

	const otherpub: [_]u8 = [
		0x04, 0xa7, 0xc7, 0x6b, 0x97, 0x0c, 0x3b, 0x5f, 0xe8, 0xb0,
		0x5d, 0x28, 0x38, 0xae, 0x04, 0xab, 0x47, 0x69, 0x7b, 0x9e,
		0xaf, 0x52, 0xe7, 0x64, 0x59, 0x00, 0x00, 0x00, 0x00, 0xe7,
		0x51, 0x32, 0x72, 0x73, 0x44, 0x66, 0xb4, 0x00, 0x09, 0x1a,
		0xdb, 0xf2, 0xd6, 0x8c, 0x58, 0xe0, 0xc5, 0x00, 0x66, 0xac,
		0x68, 0xf1, 0x9f, 0x2e, 0x1c, 0xb8, 0x79, 0xae, 0xd4, 0x3a,
		0x99, 0x69, 0xb9, 0x1a, 0x08, 0x39, 0xc4, 0xc3, 0x8a, 0x49,
		0x74, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x24, 0x34, 0x51, 0x91,
		0x5e, 0xd0, 0x90, 0x5a, 0x32, 0xb0, 0x60, 0x99, 0x2b, 0x46,
		0x8c, 0x64, 0x76, 0x6f, 0xc8, 0x43, 0x7a,
	];

	let dpub: [ec::P384_POINTSZ]u8 = [0...];
	pubkey(dpub, &priv);
	assert(bytes::equal(dpub, pub));

	let dshared: [P384_SHAREDSZ]u8 = [0...];
	let zero: [P384_SHAREDSZ]u8 = [0...];

	assert(derive(dshared, &priv, otherpub) is invalidkey);
	assert(bytes::equal(dshared, zero));
};

@test fn p521_invalidpoint() void = {
	let priv = p521();
	priv.x[..] = [
		0x01, 0x7e, 0xec, 0xc0, 0x7a, 0xb4, 0xb3, 0x29, 0x06, 0x8f,
		0xba, 0x65, 0xe5, 0x6a, 0x1f, 0x88, 0x90, 0xaa, 0x93, 0x5e,
		0x57, 0x13, 0x4a, 0xe0, 0xff, 0xcc, 0xe8, 0x02, 0x73, 0x51,
		0x51, 0xf4, 0xea, 0xc6, 0x56, 0x4f, 0x6e, 0xe9, 0x97, 0x4c,
		0x5e, 0x68, 0x87, 0xa1, 0xfe, 0xfe, 0xe5, 0x74, 0x3a, 0xe2,
		0x24, 0x1b, 0xfe, 0xb9, 0x5d, 0x5c, 0xe3, 0x1d, 0xdc, 0xb6,
		0xf9, 0xed, 0xb4, 0xd6, 0xfc, 0x47,
	];
	const pub: [_]u8 = [
		0x04, 0x00, 0x60, 0x2f, 0x9d, 0x0c, 0xf9, 0xe5, 0x26, 0xb2,
		0x9e, 0x22, 0x38, 0x1c, 0x20, 0x3c, 0x48, 0xa8, 0x86, 0xc2,
		0xb0, 0x67, 0x30, 0x33, 0x36, 0x63, 0x14, 0xf1, 0xff, 0xbc,
		0xba, 0x24, 0x0b, 0xa4, 0x2f, 0x4e, 0xf3, 0x8a, 0x76, 0x17,
		0x46, 0x35, 0xf9, 0x1e, 0x6b, 0x4e, 0xd3, 0x42, 0x75, 0xeb,
		0x01, 0xc8, 0x46, 0x7d, 0x05, 0xca, 0x80, 0x31, 0x5b, 0xf1,
		0xa7, 0xbb, 0xd9, 0x45, 0xf5, 0x50, 0xa5, 0x01, 0xb7, 0xc8,
		0x5f, 0x26, 0xf5, 0xd4, 0xb2, 0xd7, 0x35, 0x5c, 0xf6, 0xb0,
		0x21, 0x17, 0x65, 0x99, 0x43, 0x76, 0x2b, 0x6d, 0x1d, 0xb5,
		0xab, 0x4f, 0x1d, 0xbc, 0x44, 0xce, 0x7b, 0x29, 0x46, 0xeb,
		0x6c, 0x7d, 0xe3, 0x42, 0x96, 0x28, 0x93, 0xfd, 0x38, 0x7d,
		0x1b, 0x73, 0xd7, 0xa8, 0x67, 0x2d, 0x1f, 0x23, 0x69, 0x61,
		0x17, 0x0b, 0x7e, 0xb3, 0x57, 0x99, 0x53, 0xee, 0x5c, 0xdc,
		0x88, 0xcd, 0x2d,
	];

	const otherpub: [_]u8 = [
		0x04, 0x00, 0x68, 0x5a, 0x48, 0xe8, 0x6c, 0x79, 0xf0, 0xf0,
		0x87, 0x5f, 0x7b, 0xc1, 0x8d, 0x25, 0xeb, 0x5f, 0xc8, 0xc0,
		0xb0, 0x7e, 0x5d, 0xa4, 0xf4, 0x37, 0x0f, 0x3a, 0x94, 0x90,
		0x34, 0x08, 0x54, 0x33, 0x4b, 0x1e, 0x1b, 0x87, 0xfa, 0x39,
		0x54, 0x64, 0xc6, 0x06, 0x00, 0x00, 0x00, 0x00, 0x70, 0xd0,
		0xf7, 0x85, 0x60, 0x1d, 0x37, 0xc0, 0x98, 0x70, 0xeb, 0xf1,
		0x76, 0x66, 0x68, 0x77, 0xa2, 0x04, 0x6d, 0x01, 0xba, 0x52,
		0xc5, 0x6f, 0xc8, 0x77, 0x6d, 0x9e, 0x8f, 0x5d, 0xb4, 0xf0,
		0xcc, 0x27, 0x63, 0x6d, 0x0b, 0x74, 0x1b, 0xbe, 0x05, 0x40,
		0x06, 0x97, 0x94, 0x2e, 0x80, 0xb7, 0x39, 0x88, 0x4a, 0x83,
		0xbd, 0xe9, 0x9e, 0x0f, 0x67, 0x16, 0x93, 0x9e, 0x63, 0x2b,
		0xc8, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xd4, 0x43, 0xa3, 0x48,
		0xb6, 0xc3, 0xe5, 0x22, 0x49, 0x79, 0x55, 0xa4, 0xf3, 0xc3,
		0x02, 0xf6, 0x76,
	];

	let dpub: [ec::P521_POINTSZ]u8 = [0...];
	pubkey(dpub, &priv);
	assert(bytes::equal(dpub, pub));

	let dshared: [P521_SHAREDSZ]u8 = [0...];
	let zero: [P521_SHAREDSZ]u8 = [0...];

	assert(derive(dshared, &priv, otherpub) is invalidkey);
	assert(bytes::equal(dshared, zero));
};
